package com.bds.btcdc;

import com.bds.btcdc.api.TradeService;
import com.bds.btcdc.web.task.AccountInfoTask;
import com.bds.btcdc.web.utils.Const;
import com.bds.btcdc.web.utils.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableScheduling
public class App {
	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(App.class);
		ApplicationContext context = springApplication.run(args);
		SpringContextHolder.setApplicationContext(context);
        ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
        //资产信息查询
        scheduled.scheduleAtFixedRate(new AccountInfoTask(), 5, 60*30, TimeUnit.SECONDS);


//		CoinService stra_one = MyApplicationContextAware.getBean(CoinService.class);
//		System.out.println(stra_one.getAllUsing().size());
//		StrategyManyService stra_many = MyApplicationContextAware.getBean(StrategyManyService.class);
//		stra_many.test();
//		StrategyOneService stra_many = MyApplicationContextAware.getBean(StrategyOneService.class);
//		stra_many.test();
//		StrategyService testService = MyApplicationContextAware.getBean(StrategyService.class);
//		testService.execute5();
//		TradeService testService = MyApplicationContextAware.getBean(TradeService.class);
//        Result zb = testService.getAccount(54L);
//        System.out.println("资产情况："+ JSONObject.toJSONString(zb));
	}

	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {

		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {

				ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/page/page404.html");
				ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/page/page404.html");
				ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/page/page500.html");

				container.addErrorPages(error401Page, error404Page, error500Page);
			}
		};
	}


	@Autowired
	Environment environment;

	@Bean
	public HessianProxyFactoryBean helloClient() {
		HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
		Const.NODE_URL="http://"+environment.getProperty("node.url")+"/TradeService";
		factory.setServiceUrl(Const.NODE_URL);
		factory.setServiceInterface(TradeService.class);
		return factory;
	}

}
